# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import json
from typing import Dict
from pydantic import TypeAdapter
from common.enums.voice import VoiceEnum
from common.utils.config import ConfigUtil
from apps.admin.schemas.setting.ai import voice_schema as schema


class AiVoiceService:

    @classmethod
    async def detail(cls):
        speech: Dict[str, schema.SpeechConfig] = {}
        transfer: Dict[str, schema.TransferConfig] = {}

        # 文本转语音
        cacheSpeech = await ConfigUtil.get("voice_speech", "configs") or {}
        for key, item in VoiceEnum.get_default_speech().items():
            config = cacheSpeech.get(key, {})
            merged = {**item, **config}
            print(merged)
            speech[key] = TypeAdapter(schema.SpeechConfig).validate_python(merged)

        # 语音转文本
        cacheTrans = await ConfigUtil.get("voice_transfer", "configs") or {}
        for key, item in VoiceEnum.get_default_transfer().items():
            config = cacheTrans.get(key, {})
            merged = {**item, **config}
            transfer[key] = TypeAdapter(schema.TransferConfig).validate_python(merged)

        # 返回结果集
        return schema.VoiceConfigVo(
            speech=schema.VoiceChannel(
                is_open=int(await ConfigUtil.get("voice_speech", "is_open") or "0"),
                channel=str(await ConfigUtil.get("voice_speech", "channel") or "xunfei"),
                configs=speech
            ),
            transfer=schema.VoiceChannel(
                is_open=int(await ConfigUtil.get("voice_transfer", "is_open") or "0"),
                channel=str(await ConfigUtil.get("voice_transfer", "channel") or "xunfei"),
                configs=transfer
            )
        )

    @classmethod
    async def save(cls, post: schema.VoiceConfigVo):
        # 文本转语音
        await ConfigUtil.set("voice_speech", "is_open", post.speech.is_open)
        await ConfigUtil.set("voice_speech", "channel", post.speech.channel)
        await ConfigUtil.set("voice_speech", "configs", json.dumps({
            "browser": {
                "open": post.speech.configs["browser"].open,
                "name": post.speech.configs["browser"].name,
                "model": post.speech.configs["browser"].model,
                "voice": post.speech.configs["browser"].voice,
                "speed": post.speech.configs["browser"].speed,
                "volume": post.speech.configs["browser"].volume
            },
            "xunfei": {
                "open": post.speech.configs["xunfei"].open,
                "name": post.speech.configs["xunfei"].name,
                "model": post.speech.configs["xunfei"].model,
                "voice": post.speech.configs["xunfei"].voice,
                "speed": post.speech.configs["xunfei"].speed,
                "volume": post.speech.configs["xunfei"].volume
            },
            "openai": {
                "open": post.speech.configs["openai"].open,
                "name": post.speech.configs["openai"].name,
                "model": post.speech.configs["openai"].model,
                "voice": post.speech.configs["openai"].voice,
                "speed": post.speech.configs["openai"].speed,
                "volume": 0,
                "agency_api": post.speech.configs["openai"].agency_api
            }
        }, ensure_ascii=False))

        # 语言转文本
        await ConfigUtil.set("voice_transfer", "is_open", post.transfer.is_open)
        await ConfigUtil.set("voice_transfer", "channel", post.transfer.channel)
        await ConfigUtil.set("voice_transfer", "configs", json.dumps({
            "xunfei": {
                "name": post.transfer.configs["xunfei"].name,
                "model": post.transfer.configs["xunfei"].model
            },
            "openai": {
                "name": post.transfer.configs["openai"].name,
                "model": post.transfer.configs["openai"].model,
                "agency_api": post.transfer.configs["openai"].agency_api
            }
        }, ensure_ascii=False))
